#include "Protocol.hpp"

enum
{
    DIVZERO = 1,
    MODZERO
};

class CalServer
{
public:
    void CalculatorWorking()
    {
        switch (_req._op)
        {
        case '+':
            _resp._result = _req._x + _req._y;
            break;
        case '-':
            _resp._result = _req._x - _req._y;
            break;
        case '*':
            _resp._result = _req._x * _req._y;
            break;
        case '/':
            {
                if(_req._y == 0)    _resp._checkcode = DIVZERO;
                else _resp._result = _req._x / _req._y;
            }
            break;
        case '%':
            {
                if(_req._y == 0)    _resp._checkcode = MODZERO;
                else _resp._result = _req._x % _req._y;
            }
            break;        
        default:
            _resp._checkcode = -1;
            break;
        }
    }

    string Calculator(string& message)  //返回字符串结果
    {
        string contents;
        if(!Decode(message, &contents))
        {
            //cout << "Decode: invalid fomat!" << endl;
            return "";
        }
        _req.Deserialization(&contents);

        contents = "";   
        CalculatorWorking();
        _resp.Serialization(&contents);
        string pacakage;
        if(!Encode(contents, &pacakage))
        {
            //cout << "Encode: invalid fomat!" << endl;
            return "";
        }
        return pacakage;
    }

public:
    Request _req;
    Response _resp;
};